package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentPersonnel;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.entity.MaintainSign;
import org.springblade.modules.repair.enums.StatusEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IMaintainPlanContentUserService;
import org.springblade.modules.repair.service.IMaintainSignService;
import org.springblade.modules.repair.service.MaintainPlanContentPersonnelService;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zcc
 * @version 1.0
 * @date 2022/12/23 16:23
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintainPlanContentUser")
@Api(value = "维保任务关联人员中间表", tags = "维保任务关联人员中间表接口")
@Slf4j
public class MaintainPlanContentUserController {

	private final MaintainPlanContentPersonnelService planContentPersonnelService;

	private final IMaintainPlanContentService maintainPlanContentService;

	private final IUserService userService;

	private final IMaintainPlanContentUserService maintainPlanContentUserService;

	private final IMaintainSignService maintainSignService;

	@GetMapping("/list/v2")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取分配人员")
	public R<List<MaintainPlanContentUser>> listV2(Long planId) {

		final MaintainPlanContent maintainPlanContent = maintainPlanContentService.getById(planId);
		if (maintainPlanContent.getStatus().equals(TaskStatusEnum.BY_DISABLE.getStatus()) || maintainPlanContent.getStatus().equals(TaskStatusEnum.BY_PROCESSED.getStatus())) {
			final List<MaintainPlanContentUser> planContentUserList = maintainPlanContentUserService.list(Wrappers.<MaintainPlanContentUser>lambdaQuery().eq(MaintainPlanContentUser::getPlanId, planId));
			for (MaintainPlanContentUser maintainPlanContentUser : planContentUserList) {
				maintainPlanContentUser.setUserName(userService.getById(maintainPlanContentUser.getUserId()).getRealName());
				maintainPlanContentUser.setDeptNames(userService.getDeptNames(maintainPlanContentUser.getUserId()));
				final List<MaintainSign> signList = maintainSignService.list(Wrappers.<MaintainSign>lambdaQuery().eq(MaintainSign::getPlanId, maintainPlanContentUser.getPlanId()).eq(MaintainSign::getUserId, maintainPlanContentUser.getUserId()));
				maintainPlanContentUser.setStatus(signList.size() > 0 ? StatusEnum.ENABLE.getId() : StatusEnum.DISABLE.getId());
			}
			return R.data(planContentUserList);
		}

		final List<MaintainPlanContentPersonnel> list = planContentPersonnelService.list(Wrappers.<MaintainPlanContentPersonnel>lambdaQuery().eq(MaintainPlanContentPersonnel::getPlanId, planId));
		List<MaintainPlanContentUser> planContentUserList = new ArrayList<>(16);
		for (MaintainPlanContentPersonnel maintainPlanContentPersonnel : list) {
			MaintainPlanContentUser maintainPlanContentUser = new MaintainPlanContentUser();
			maintainPlanContentUser.setUserName(userService.getById(maintainPlanContentPersonnel.getUserId()).getRealName());
			maintainPlanContentUser.setDeptNames(userService.getDeptNames(maintainPlanContentPersonnel.getUserId()));
			planContentUserList.add(maintainPlanContentUser);
		}
		return R.data(planContentUserList);
	}
}
